< previous page page_427 next page >

Page 427
BOOL ReadEventLog(HANDLE hEventLog, DWORD dwReadFlags, DWORD dwRecordOffset,
LPVOID lpBuffer, DWORD nNumberOfBytesToRead, DWORD *pnBytesRead, DWORD
*pnMinNumberOfBytesNeeded );

BOOL CloseEventLog(HANDLE hEventLog);
The Visual Basic equivalents are also found in the api32.txt file. The conversion here is routine. In the OpenEventLog function, the LPCTSTR parameters are string pointers to standard NULL-terminated C strings. Because the function has a string, you can expect it to have two separate entry points: OpenEventLogA, which will use ANSI strings, and OpenEventLogW, which will use Unicode strings. The T in LPCTSTR indicates that the string is Unicode or ANSI depending on which entry point you use. Since we'll be using the ANSI entry point for now, the declaration is as follows:
Declare Function OpenEventLog Lib "advapi32.dll" Alias "OpenEventLogA" _
(ByVal lpUNCServerName As String, ByVal lpSourceName As String) As Long
The ReadEventLog function is somewhat more complex. The HANDLE and DWORD parameters are 32-bit values that are passed by value and are thus defined as Longs. The DWORD * parameters are pointers to 32-bit parameters. This means that they are also Longs, but are passed by referencethus the VB declaration leaves out the ByVal for those cases. The lpBuffer parameter is defined in api32.txt as an EVENTLOGRECORD structure, but has been redefined here as a byte that is passed by reference. You'll see why this is necessary shortly. The CloseEventLog function simply passes the Long handle parameter by value.
Declare Function ReadEventLog Lib "advapi32.dll" Alias "ReadEventLogA" _
(ByVal hEventLog As Long, ByVal dwReadFlags As Long, ByVal dwRecordOffset _
As Long, lpBuffer As Byte, ByVal nNumberOfBytesToRead As Long, pnBytesRead _
As Long, pnMinNumberOfBytesNeeded As Long) As Long

Declare Function CloseEventLog Lib "advapi32.dll" _
(ByVal hEventLog As Long) As Long
Building Code Step by Step
I've found that the easiest way to convert unfamiliar C code to Visual Basic is to do it step by step, verifying the code along the way. The Event sample has a command button that will run the code and a list box to display results. The first cut contains the following code:

 
< previous page page_427 next page >